/*
 * main.c

 *
 *  Created on: Nov 19, 2013
 *      Author: adijoshi
 */
/* -------------------------------------------------------------------------------------------------
 File - cache.h
 Type - header file
 Contents -
 	 This file contains various declarations
 ------------------------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cache.h"

void exec_Traces();

int main()
{
	init_Cache();
	exec_Traces();
	return 0;
}

void exec_Traces()
{
	unsigned int accessType;
	unsigned int address;
	FILE *input_file_ptr;
	input_file_ptr = fopen(INPUT_FILE_NAME,"r");

	if(input_file_ptr == NULL)
	{
		printf("Error while opening the file.\n");
	}
	else
	{
	while(fscanf(input_file_ptr,"%d %x\n",&accessType,&address)!=EOF)
	{
		switch(accessType){
		case CPU_READ_D:
			exec_Read_Req(CPU_READ_D,address);
			break;
		case CPU_READ_I:
			exec_Read_Req(CPU_READ_I,address);
			break;
		case CPU_WRITE_D:
			exec_Write_Req(CPU_WRITE_D,address);
			break;
		case SNOOPED_INVALIDATE:
			put_Snoop_Result('I',address);
			break;
		case SNOOPED_READ:
			put_Snoop_Result('R',address);
			break;
		case SNOOPED_WRITE:
			put_Snoop_Result('W',address);
			break;
		case SNOOPED_RFO:
			put_Snoop_Result('M',address);
			break;
		case CLEAR_CACHE:
			clear_Cache_Contents();
			break;
		case PRINT_CACHE:
			print_Cache_Contents();
			break;
		default:
			break;
		}
	}
	fclose(input_file_ptr);

	 print_Cache_Stats();
	}
	getchar();
}
